<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-if="showSearch">
      <el-form-item label="用户名称" prop="userName" label-width="100px;">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="上级用户名称" prop="superiorUserName">
        <el-input v-model="queryParams.superiorUserName" placeholder="请输入上级用户名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="SN码" prop="snCode">
        <el-input v-model="queryParams.snCode" placeholder="请输入终端SN码" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总交易笔数" prop="tradeTotal">
        <el-input v-model="queryParams.tradeTotal" placeholder="请输入总交易笔数" clearable size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总交易金额" prop="transAmount">
        <el-input v-model="queryParams.transAmount" placeholder="请输入总交易金额" clearable size="small"
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="激活状态" prop="checkStatus">
        <el-select v-model="queryParams.activateStatus" placeholder="请选择激活状态" clearable size="small">
          <el-option label="--请选择--" value="" />
          <el-option v-for="item in statusList" :key="item.code" :value="item.code" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定状态" prop="checkStatus">
        <el-select v-model="queryParams.bindingStatus" placeholder="请选择绑定状态" clearable size="small">
          <el-option label="--请选择--" value="" />
          <el-option v-for="item in statusList" :key="item.code" :value="item.code" :label="item.show" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="queryAddTimeStr">
        <el-date-picker
          v-model="queryParams.queryAddTimeStr"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['pos:TTerminalInfo:add']">新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="handleBatchTransfer"
          v-hasPermi="['pos:TTerminalInfo:edit']">划拨</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pos:TTerminalInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['pos:TTerminalInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="TTerminalInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="手机号" show-overflow-tooltip align="center" prop="phone" />
      <el-table-column label="终端品牌" align="center" prop="brandName" />
      <el-table-column label="终端SN码" align="center" prop="snCode" />
      <el-table-column label="直属上级" align="center" prop="superiorUserName" />
      <el-table-column label="绑定状态" align="center" prop="bindingStatusShow" />
      <el-table-column label="激活状态" align="center" prop="activateStatusShow" />
      <el-table-column label="总交易笔数" align="center" prop="tradeTotal" />
      <el-table-column label="总交易金额" align="center" prop="transAmount" />
      <el-table-column label="创建时间" align="center" prop="addTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row,false)"
            v-hasPermi="['pos:TTerminalInfo:edit']">详情</el-button>
          <!--<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row,true)" v-if="scope.row.transStatus != '20'? true : false"-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row,true)"
            v-hasPermi="['pos:TTerminalInfo:edit']">划拨</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pos:TTerminalInfo:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-if="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户商户终端关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="!submitBtnFlag?'800px':'650px'" append-to-body>
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="115px">
        <el-form-item label="用户名称" prop="userName">
          <el-select :disabled="!submitBtnFlag" v-model="form.userId" placeholder="请选择用户"  style="width: 203px;" filterable clearable size="small">
            <el-option v-for="item in TUserInfoList" :key="item.userId" :label="item.userName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :disabled="!submitBtnFlag" label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="手机号" prop="phone">
          <el-input disabled v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="终端SN码" prop="snCode">
          <el-input disabled v-model="form.snCode" placeholder="请输入终端SN码" />
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="直属上级" prop="superiorUserName">
          <el-input disabled v-model="form.superiorUserName" placeholder="请输入直属上级" />
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="激活状态">
          <el-radio-group disabled style="width: 203px;" v-model="form.activateStatus">
            <el-radio v-for="dict in dict.type.activateStatus" :key="dict.value" :label="dict.value">{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="绑定状态">
          <el-radio-group disabled style="width: 203px;" v-model="form.bindingStatus">
            <el-radio v-for="dict in dict.type.bindingStatus" :key="dict.value" :label="dict.value">{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="总交易笔数" prop="tradeTotal">
          <el-input disabled v-model="form.tradeTotal" placeholder="请输入总交易笔数" />
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="总交易金额" prop="transAmount">
          <el-input disabled v-model="form.transAmount" placeholder="请输入总交易金额" />
        </el-form-item>
        <el-form-item v-if="!submitBtnFlag" label="创建时间" prop="addTime">
          <el-date-picker disabled clearable size="small" style="width: 203px;" v-model="form.addTime" type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss" placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>

        <el-table v-if="!submitBtnFlag" :data="form.userDefaultInfoList" border show-summary style="width: 100%">
          <el-table-column type="index" width="50">
          </el-table-column>
          <el-table-column label="类型">
            <template slot-scope="scope">
              {{(scope.row.transType=='20'&&scope.row.transOperType=='2001')?' 积分奖励':((scope.row.transType=='10'&&scope.row.transOperType=='1004')?'激活奖励':'')}}
            </template>
          </el-table-column>
          <el-table-column prop="transAmount" sortable label="积分">
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="submitBtnFlag" @click="submitForm" v-preventReClick>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    queryStatus,
    listTTerminalInfo,
    getTTerminalInfo,
    delTTerminalInfo,
    addTTerminalInfo,
    updateTTerminalInfo,
    exportTTerminalInfo,
    transfer
  } from "@/api/pos/TTerminalInfo";
  import {
    listForTrans,
  } from "@/api/pos/TUserInfo";
  import {
    recordList,
  } from "@/api/pos/TUserDefaultInfo";


  export default {
    name: "TTerminalInfo",
    dicts: ['terminalType', 'sellStatus', 'activateStatus', 'bindingStatus'],
    data() {
      return {
        statusList: [],
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户商户终端关系表格数据
        TTerminalInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: null,
          userName: null,
          superiorUserName: null,
          phone: null,
          tradeTotal: null,
          transAmount: null,
          merchant: null,
          dealSum: null,
          terminalType: null,
          brandId: null,
          commodityId: null,
          terminalModel: null,
          snCode: null,
          sellStatus: null,
          activateStatus: null,
          bindingStatus: null,
          addTime: null,
          sellTime: null,
          activateTime: null,
          bindingTime: null,
          terminalGmv: null,
          queryAddTimeStr: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 是否显示提交按钮
        submitBtnFlag: false,
        // 用户列表
        TUserInfoList: [],
      };
    },
    created() {
      this.getList();
      this.getStatus();
    },
    methods: {
      // 用户下拉框改变事件
      userChange(event) {
        let user = this.TUserInfoList.filter((item) => item.userId === event)[0];
        this.form.superiorUserName = user.superiorUserName;
        recordList({
          userId: event
        }).then(res => {
          this.form.userDefaultInfoList = res.data;
        })
      },
      /** 查询用户商户终端关系列表 */
      getList() {
        this.loading = true;
        listTTerminalInfo(this.queryParams).then(response => {
          this.TTerminalInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getStatus(){
        queryStatus().then(response => {
          this.statusList = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: null,
          userName: null,
          phone: null,
          merchant: null,
          dealSum: null,
          terminalType: null,
          brandId: null,
          commodityId: null,
          terminalModel: null,
          snCode: null,
          sellStatus: "0",
          activateStatus: "0",
          bindingStatus: "0",
          addTime: null,
          sellTime: null,
          activateTime: null,
          bindingTime: null,
          terminalGmv: null,
          brand: null,
          goodsName: null,
          superiorUserName: null,
          userDefaultInfoList: [],
          tradeTotal: null,
          transAmount: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户商户终端关系";
      },
      /** 修改按钮操作 */
      handleUpdate(row, flag) {
        this.submitBtnFlag = flag;
        this.reset();
        const id = row.id || this.ids
        getTTerminalInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = flag ? "划拨" : "详情";
        });
        listForTrans(null).then(response => {
          this.TUserInfoList = response.rows;
        });
      },
      handleBatchTransfer() {
        this.submitBtnFlag = true;
        this.reset();
        listForTrans(null).then(response => {
          this.title = "划拨";
          this.open = true;
          this.TUserInfoList = response.rows;
        });
      },
      /** 提交按钮 */
      submitForm(row) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.ids = this.ids;
            if (this.form.id != null || this.form.ids != null) {
              transfer(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTTerminalInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除？').then(function() {
          return delTTerminalInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有用户商户终端关系数据项？').then(() => {
          this.exportLoading = true;
          return exportTTerminalInfo(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
